﻿
//===============================================================================
//	Player.h
//	用户类
//===============================================================================

#include "Player.h"

#include "ManagerHelper.h"

#include "../share/user_define.h"

#include "../share/MessageDef.h"


//#include "../share/StringFactory.h"
#include "ConstantDefine.h"

#include <boost/lexical_cast.hpp>

//-------------------------------------------------------------------------------
#include "CGeneralJsonStringRet.h"
#include "CPing.h"
#include "CEnterScene.h"
#include "CLeaveScene.h"
#include "CPlayerLoadSceneFinished.h"
#include "CPlayerMove.h"
#include "CKick.h"

#include "CLoginMapSrvRet.h"
#include "MapServer.h"
#include "../share/time_module.h"
#include "map_playermgr.h"
#include "scene_manager.h"
#include "entity_define.h"
#include "MonsterTable.h"
#include "../share/proxyclient_service.h"

//-------------------------------------------------------------------------------
void Player::createSocketMessage(void)
{
	_safe_guard_begin;

	m_ping = dynamic_cast<CPing*>(
		MsgFactoryPtr->CreateMessage(this->m_socket_id, player_map_ping_ret));
	_safe_check(m_ping);

	m_enter_scene = dynamic_cast<CEnterScene*>(
		MsgFactoryPtr->CreateMessage(this->m_socket_id, c_player_enter_scene));
	_safe_check(m_enter_scene);

	m_leave_scene = dynamic_cast<CLeaveScene*>(
		MsgFactoryPtr->CreateMessage(this->m_socket_id, c_player_leave_scene));
	_safe_check(m_leave_scene);

	m_player_load_scene_finished = dynamic_cast<CPlayerLoadSceneFinished*>(
		MsgFactoryPtr->CreateMessage(this->m_socket_id, c_player_load_scene_finished));
	_safe_check(m_player_load_scene_finished);

	m_login_mapsrv_ret = dynamic_cast<CLoginMapSrvRet*>(
		MsgFactoryPtr->CreateMessage(this->m_socket_id, map_player_login_ret));
	_safe_check(m_login_mapsrv_ret);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::deleteSocketMessage(void)
{
	_safe_guard_begin;

	MsgFactoryPtr->DeleteMessage(m_login_mapsrv_ret);

	MsgFactoryPtr->DeleteMessage(m_player_load_scene_finished);
	MsgFactoryPtr->DeleteMessage(m_leave_scene);
	MsgFactoryPtr->DeleteMessage(m_enter_scene);
	MsgFactoryPtr->DeleteMessage(m_ping);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::clearPlayerInfo(void)
{
	_safe_guard_begin;
	memset(m_Name, 0, 32);				// 昵称
	memset(m_UnionName, 0, 64);

	m_PlayerId = 0;
	m_Account = 0;				// 帐号
	m_Area = 0;					// 所在游戏区
	m_Photo = 0;				// 头像ID
	m_PhotoFrame = 0;			// 头像框ID
	m_Exps = 0;					// 当前经验值
	m_MaxExps = 0;				// 当前最大经验值
	m_Level = 0;				// 当前等级
	m_HeroId = 0;				// 当前英雄ID
	m_Vip = 0;					// VIP等级
	m_VipExps = 0;				// VIP经验
	m_UnionId = 0;				// 工会ID
	m_UnionPosition = 0;		// 工会职位
	m_Afc = 0;					// 战队战斗力


	m_PetId = 0;				// 宠物ID
	m_MountsId = 0;				// 坐骑ID
	m_MountState = 0;				//坐骑骑乘状态
	m_CurrHeroLevel = 1;		// 当前出阵英雄等级

	m_DefPetId = 0;				// 默认设定宠物ID
	m_DefMountsId = 0;			// 默认设定坐骑ID

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::sendPlayerLoadSceneFinished(void)
{
	_safe_guard_begin;

	m_player_load_scene_finished->Clear();
	m_player_load_scene_finished->SetPlayer(this);

	SendToClient(m_player_load_scene_finished->GetSendString(),
		m_player_load_scene_finished->GetProtocolNumber());

	_safe_guard_end;
}

void Player::SendPlayerLoginMapSrvResult(void)
{
	_safe_guard_begin;

	//m_login_mapsrv_ret->Clear();
	//m_login_mapsrv_ret->SetJsonData("ret", 0);

	//SendToClient(m_login_mapsrv_ret->GetSendString(),
	//	m_login_mapsrv_ret->GetProtocolNumber());


	//Json::Value clear_jv;
	//clear_jv["msgid"] = map_player_login_req;
	//clear_jv["ret"] = ::common::SUCCEED;
	//clear_jv["playerId"] = _playerId;
	//clear_jv["heroId"] = _heroId;
	//clear_jv["skinId"] = _skinId;
	//clear_jv["playerLv"] = _playerLv;
	//std::string send_str = JsonWriter(clear_jv);
	//player->SendToClient(&send_str, map_player_login_req);
	_safe_guard_end;
}

void Player::SerialToJson(Json::Value& baseinfo)
{
	baseinfo.clear();
	baseinfo["si"] = GetSceneId();
	baseinfo["px"] = position().x();
	baseinfo["pz"] = position().z();
	baseinfo["py"] = position().y();
	baseinfo["td"] = 0;
	baseinfo["tp"] = 0;
	baseinfo["ky"] = GetKey();
	baseinfo["ai"] = GetAccount();
	baseinfo["pi"] = GetPlayerId();
	baseinfo["ri"] = GetHeroId();
	baseinfo["tl"] = 0;
	baseinfo["nm"] = std::string(GetName());
	baseinfo["mountsState"] = std::to_string(GetMountsState());
	baseinfo["unId"] = GetUnionId();
	baseinfo["unNm"] = GetUnionName();
	baseinfo["pt"] = GetPetId();
	baseinfo["mt"] = GetMountsId();
	baseinfo["skinid"] = GetHeroSkinId();
	baseinfo["areaId"] = GetArea();
	baseinfo["rc"] = 0;
}

int32_t Player::GetSceneId()
{
	CSceneInstance* instance = CSceneManager::GetInstance().GetSceneInstance(instance_id());
	if ( instance == NULL )
	{
		return 20000;
	}
	return instance->tpl_id();
}

//-------------------------------------------------------------------------------
Player::Player(void)
	: m_Afc(0)
	, m_Level(0)
	, m_Vip(0)
	, player_link_(this)
{
	_safe_guard_begin;

	clearPlayerInfo();
	for (uint32_t i = 0; i < max_hero_count; ++i )
	{
		m_playerHero[i].Clear();
	}

	createSocketMessage();

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
Player::~Player(void)
{
	deleteSocketMessage();
}


void Player::TickUpdate()
{
	if (!CheckPingTime())
	{
		LOG_INFO("default", "KICK PLAYER !!!! CheckPingTime kick : [%u][%u]",
			GetKey(), GetPlayerId());

		CMapPlayerManger::GetInstance().PlayerLeaveGame(*this);
		return;
	}

}

//-------------------------------------------------------------------------------
void Player::Send2CEnterScene(uint32_t sceneId, int32_t ret_code)
{
	_safe_guard_begin;

	uint32_t accoutId = GetAccount();

	//m_enter_scene->Clear();
	//m_enter_scene->SetSceneAndPosition(sceneId, posX, posY, posZ, accoutId);

	Json::Value m_jv;
	m_jv["si"] = sceneId;
	m_jv["px"] = position().x();
	m_jv["py"] = position().y();
	m_jv["pz"] = position().z();
	m_jv["ai"] = accoutId;
	m_jv["ret"] = ret_code;
	m_jv["msgid"] = c_player_enter_scene;

	std::string send_str = JsonWriter(m_jv);
	SendToClient(&send_str, c_player_enter_scene);

	LOG_DEBUG("default", "c_player_enter_scene to %d|%d -> sendStr: %s",
		m_Account, m_PlayerId, m_enter_scene->GetSendToServerString()->c_str());

	_safe_guard_end;

}

//-------------------------------------------------------------------------------
void Player::Send2CLevelScene(uint32_t sceneId)
{
	_safe_guard_begin;

	m_leave_scene->Clear();
	m_leave_scene->SetNewSceneId(sceneId);

	SendToClient(m_leave_scene->GetSendString(), m_leave_scene->GetProtocolNumber());

	LOG_DEBUG("default","c_player_leave_scene to %d|%d -> sendStr: %s",
		m_Account, m_PlayerId, m_leave_scene->GetSendToServerString()->c_str());

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
uint32_t Player::GetSocketId(void)
{
	_safe_guard_begin;

	return m_socket_id;

	_safe_guard_end;

}

//-------------------------------------------------------------------------------
void Player::SetSocketId(uint32_t socketId)
{
	_safe_guard_begin;

	m_socket_id = socketId;

	_safe_guard_end;
}


//-------------------------------------------------------------------------------
void Player::SendToClient(std::string* str, uint32_t msgId)
{
	_safe_guard_begin;

	if (m_socket_id >= 0)
	{
		LOG_DEBUG("default", "SendToClient Player: %d, msgId: 0x%04X(%u), len %d", this->GetPlayerId(), msgId, msgId, str->size());

		MapServer::GetInstance().client_msg_dispatch(*str, m_socket_id, msgId);
	}
	else
	{
		LOG_ERROR("default","m_socket_id is 0, can't send %d ...", msgId);
	}

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::SetName(const char* v)
{
	_safe_guard_begin;

	memset(m_Name, 0, 32);
	memcpy(m_Name, v, strlen(v));

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
char* Player::GetName(void)
{
	_safe_guard_begin;

	return m_Name;

	_safe_guard_end;

	return nullptr;
}

void Player::SetUnionName(const char * v)
{
	_safe_guard_begin;

	memset(m_UnionName, 0, 32);
	memcpy(m_UnionName, v, strlen(v));

	_safe_guard_end;
}

char * Player::GetUnionName(void)
{
	_safe_guard_begin;

	return m_UnionName;

	_safe_guard_end;

	return nullptr;
}


//-------------------------------------------------------------------------------
void Player::SetPingTime(uint32_t ch, uint32_t cl)
{
	_safe_guard_begin;

	uint64_t _st = CUR_MS;
	last_ping_time_ = CUR_SEC;
	m_ping->Clear();
	m_ping->SetPingTime(ch, cl, _st);

	//LOG_DEBUG("default","Player::SPing ret[%d]: %s", m_ping->GetProtocolNumber(), m_ping->GetSendToServerString()->c_str());
	//SendToClient(m_ping->GetSendToServerString(), m_ping->GetProtocolNumber());

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
bool Player::CheckPingTime(void)
{
	_safe_guard_begin;

	int32_t _now_time = CUR_SEC;

	if (0 == last_ping_time_)
	{
		last_ping_time_ = _now_time;
		return true;
	}

	if (_now_time - last_ping_time_ < 60*10 )
	{
		return true;
	}

	return false;

	_safe_guard_end;

	return false;
}

//-------------------------------------------------------------------------------
void Player::ChangeScene(int32_t flag, uint32_t to, uint32_t from /*= 0*/)
{
	_safe_guard_begin;

 	uint32_t _current_scene_id = GetSceneId();

	if (_current_scene_id == 0)
	{
		LOG_WARN("default","ChangeScene _current_scene_id=0");
		return;
	}

// 	Map* _map = GetMapPtr();
// 	if (nullptr == _map)
// 	{
// 		_map = MapManagerPtr->GetScenePtrBySceneKey(_current_scene_id);
// 	}
// 	_safe_check(_map);
// 
// 	if (0 == from)
// 	{
// 		from = _current_scene_id;
// 	}
// 
// 	MapSlot::GetInstance().playerLeaveScene(m_PlayerId, from);
// 	MapSlot::GetInstance().playerEnterScene(m_PlayerId);

//	_map->ChangeScene(GetKey(), GetSn(), flag, from, to);
	
	_safe_guard_end;
}


uint32_t Player::GetPlayerHp(void)
{
	

	FightProperty& _fp = GetFightProperty();
	return _fp.GetHp();
}

bool Player::SetCurrentHp(int32_t blood) {
	if (blood < 0) {
		blood = 0;
	}
	m_hp = blood;
	return true;
}
//-------------------------------------------------------------------------------
void Player::ModifyHp(uint32_t srcKey, uint32_t tarKey, int32_t val)
{
	_safe_guard_begin;

	FightProperty& _fp = GetFightProperty();

	int32_t _hp = _fp.GetHp();
	_hp -= val;
	if (_hp <= 0)
	{
		_hp = 0;
		if (PlayerState::ps_dead != GetPlayerState())
		{
			SetPlayerState(PlayerState::ps_dead);

			//玩家挂了(被掉血了).
			BroadcastSomeoneDead(srcKey, tarKey, 0);
		}
	}

	int32_t _max_hp = GetFightProperty().GetMaxHp();
	if (_hp > _max_hp)
	{
		_hp = _max_hp;
	}

	_fp.SetHp(_hp);


	uint32_t flag = 0;
	flag |= (uint32_t)UpdatePlayerInfoType::Hp;
	CSceneInstance::Broadcast(*this, flag);
	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::BroadcastSomeoneDead(uint32_t sourceKey, uint32_t targetKey, uint32_t reviveCount)
{
	_safe_guard_begin;


	Json::Value dead_msg;
	dead_msg["sk"] = sourceKey;
	dead_msg["tk"] = targetKey;
	dead_msg["rc"] = reviveCount;
	dead_msg["msgid"] = c_someone_dead_ret;

	CSceneInstance* ins = CSceneManager::GetInstance().GetSceneInstance(instance_id());
	if (ins == NULL) {
		return;
	}
	ins->BroadcastViewMessage(*this, c_someone_dead_ret, dead_msg, 0);

	_safe_guard_end;
}


bool Player::PlayerInGame()
{
	return PlayerState::ps_pre_enter_scene == m_player_state ? false : true;
}

//-------------------------------------------------------------------------------
void Player::KickPlayerNotify(uint32_t kickType)
{
	_safe_guard_begin;

	if (m_socket_id < 0)
	{
		return;
	}
	//	踢人前通知
	m_kick->Clear();
	m_kick->SetKickPlayer(this->GetPlayerId(), kickType);//0:默认,1:重复登录,2:服务器关闭
	SendToClient(m_kick->GetSendToServerString(), m_kick->GetProtocolNumber());

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::KilledSomeone(CEntity* tar)
{
	_safe_guard_begin;

	//if (
	//	GOT_MONSTER == tar->GetObjectType() ||
	//	GOT_RAND_MONSTER == tar->GetObjectType()
	//	)
	//{
	//	///检查悬赏任务：杀怪脚本
	//	uint32_t _typeId = tar->GetTypeId();//场景元素类型id
	//										//uint32_t _typeId = tar->GetKey();//地图元素识别id

	//	if (nullptr == m_player_lua_ptr)
	//	{
	//		return;
	//	}
	//	_safe_check(m_player_lua_ptr != nullptr);

	//	uint32_t _usrPosId = GetSn();
	//	if (m_player_lua_ptr->TryLock())
	//	{
	//		m_player_lua_ptr->RunScriptFunction(LUA_OfferRewardKillMonsterScript
	//			, LUA_KillMonster, _usrPosId, _typeId);
	//	}
	//	else
	//	{
	//		m_player_lua_ptr->RunScriptFunction(LUA_OfferRewardKillMonsterScript
	//			, LUA_KillMonster, _usrPosId, _typeId);
	//		LOG_ERROR("default", "TryLock FALSE! KilledSomeone 1");
	//	}

	//	///检查主线杀怪/-掉落任务
	//	KillMonsterMainMission(_typeId);
	//}
	//else if (GOT_PLAYER == tar->GetObjectType())
	//{
	//	if (nullptr == m_player_lua_ptr)
	//	{
	//		return;
	//	}
	//	///悬赏任务：杀人脚本
	//	_safe_check(m_player_lua_ptr != nullptr);

	//	uint32_t _usrPosId = GetSn();
	//	if (m_player_lua_ptr->TryLock())
	//	{
	//		m_player_lua_ptr->RunScriptFunction(LUA_OfferRewardKillPlayerScript
	//			, LUA_KillPlayer, (int32_t)_usrPosId);
	//	}
	//	else
	//	{
	//		m_player_lua_ptr->RunScriptFunction(LUA_OfferRewardKillPlayerScript
	//			, LUA_KillPlayer, (int32_t)_usrPosId);
	//		LOG_ERROR("default", "TryLock FALSE! KilledSomeone 2");
	//	}
	//}
	Json::Value target;
	target["msgid"] = m2g_kill_some_one;
	target["ret"] = ::common::SUCCEED;
	target["target_type"] = tar->GetObjectType();

	std::string send_str = JsonWriter(target);
	send_2_game(m2g_kill_some_one, send_str, GetArea());

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
//	设置Player在场景内的信息
void Player::InitializePlayerSceneInfo(bool isReset)
{
	_safe_guard_begin;

	if (!isReset)
	{
		return;
	}

	PlayerHero* _tmp_heroinfo = GetPlayerHeroInfoByID(m_HeroId);
	if (nullptr == _tmp_heroinfo)
	{
		return;
	}

	FightProperty& _fp = GetFightProperty();

	for (int16_t i = int16_t(property_type::hp); i < int16_t(property_type::count); ++i)
	{
		_fp.SetFixedProperty(property_type(i),
			int32_t(GetMainHeroProperty()->GetProperty((property_type(i)))));
	}

	//	等级
	_fp.SetFixedProperty(property_type::level, m_CurrHeroLevel);
	_safe_guard_end;
}

PlayerHero* Player::GetPlayerHeroInfoByID(uint32_t heroId)
{
	_safe_guard_begin;

	for (uint32_t i = 0; i < max_hero_count; ++i )
	{
		if (m_playerHero[i].mHeroId == heroId)
		{
			return &m_playerHero[i];
		}
	}

	//TODO 未找到英雄数据，同步一次英雄数据，下次可以找到 不安全的做法  这里不能提前判断错误
	SyncHeroGame2Map(heroId);

	return nullptr;

	_safe_guard_end;

	return nullptr;
}

//-------------------------------------------------------------------------------
HeroProperty * Player::GetMainHeroProperty(void)
{
	_safe_guard_begin;

	PlayerHero* _tmp_heroinfo = GetPlayerHeroInfoByID(m_HeroId);
	if (nullptr == _tmp_heroinfo)
	{
		return nullptr;
	}

	return &(_tmp_heroinfo->m_property);

	_safe_guard_end;

	return nullptr;
}

//-------------------------------------------------------------------------------
void Player::TargetZeroBloodNotify(uint32_t tarId)
{
	_safe_guard_begin;

	Json::Value m_jv;
	m_jv["msgid"] = c_target_zero_blood_push;
	m_jv["ret"] = ::common::SUCCEED;
	m_jv["tk"] = tarId;

	std::string msg_str = JsonWriter(m_jv);
	SendToClient(&msg_str, c_target_zero_blood_push);
	_safe_guard_end;
}

bool Player::IsDeaded()
{
	return (PlayerState::ps_dead == GetPlayerState());
}


PlayerHero* Player::GetPlayerHeroEmptyPos()
{
	_safe_guard_begin;

	for (uint32_t i = 0; i < max_hero_count; ++i )
	{
		if (m_playerHero[i].mHeroId == 0)
			return &m_playerHero[i];
	}

	return nullptr;

	_safe_guard_end;

	return nullptr;
}

void Player::SetOneHeroInfo(uint32_t heroId, Json::Value& jm)
{
	_safe_guard_begin;

	PlayerHero* hero = GetPlayerHeroInfoByID(heroId);
	if (nullptr == hero)
	{
		hero = GetPlayerHeroEmptyPos();
	}

	if (nullptr != hero)
	{
		//hero->mHeroId = jm["heroId"].asUInt();	
		//hero->mLevel = jm["level"].asUInt();
		//hero->mExps = jm["exps"].asUInt();
		//hero->mStar = jm["star"].asUInt();
		//hero->mGrade = jm["grade"].asUInt();
		//hero->mFightingCapacity = jm["fc"].asUInt();

		//hero->mUUID = jm["uuid"].asString();
		//hero->mName = jm["name"].asString();

		//hero->mEquipment = JsonWriter(jm["equipment"]);
		//hero->mRune = JsonWriter(jm["runes"]);
		//hero->mSkill = JsonWriter(jm["skill"]);

		//(hero->m_property).ResetBasePropertyDataList(jm["baseAttr"]);

		hero->mHeroId = jm["heroId"].asUInt();
		//hero->mHeroId = atoi(jm["heroId"].asCString());
		hero->mLevel = jm["level"].asUInt();
		hero->mExps = jm["exps"].asUInt();
		hero->mStar = jm["star"].asUInt();
		hero->mGrade = jm["grade"].asUInt();
		hero->mFightingCapacity = jm["fc"].asUInt();
		hero->mSkinId = jm["skinId"].asUInt();

		//hero->mUUID = jm["uuid"].asString();//被删减？
		hero->mName = jm["name"].asString();

		hero->mEquipment = JsonWriter(jm["equipment"]);
		hero->mRune = JsonWriter(jm["runes"]);
		hero->mSkill = JsonWriter(jm["skill"]);

		if (jm["baseAttr"].size() > 0) {
			(hero->m_property).ResetBasePropertyDataList(jm["baseAttr"]);
		}

		//hero->m_property->ResetEquipmentPropertyDataList(jm["equipAttr"]);
		//hero->m_property->ResetRunePropertyDataList(jm["runeAttr"]);

		SendToGameUpdateHeroInfo(hero->mHeroId);
	}

	_safe_guard_end;
}

void Player::SyncHeroGame2Map(uint32_t hero_id)
{
	Json::Value msg_json;
	msg_json["msgid"] = M2G_SYNC_HERO_REQUEST;
	msg_json["playerId"] = m_PlayerId;
	msg_json["ret"] = ::common::SUCCEED;
	msg_json["heroId"] = hero_id ? hero_id : m_HeroId;
	std::string msg_str = JsonWriter(msg_json);
	send_2_game(M2G_SYNC_HERO_REQUEST, msg_str, GetArea());
}

void Player::ResetOneHeroInfo(uint32_t heroId, Json::Value& jm)
{
	_safe_guard_begin;

	PlayerHero* hero = GetPlayerHeroInfoByID(heroId);
	if (nullptr != hero)
	{
		hero->mLevel = jm["level"].asUInt();
		hero->mExps = jm["exps"].asUInt();
		hero->mStar = jm["star"].asUInt();
		hero->mGrade = jm["grade"].asUInt();
		hero->mFightingCapacity = jm["fc"].asUInt();

		hero->mEquipment = JsonWriter(jm["equipment"]);
		hero->mRune = JsonWriter(jm["runes"]);
		hero->mSkill = JsonWriter(jm["skill"]);

		if (jm["baseAttr"].size() > 0) {
			(hero->m_property).ResetBasePropertyDataList(jm["baseAttr"]);
		}
	}

	_safe_guard_end;
}

void Player::SendToGameUpdateHeroInfo(uint32_t heroid)
{
	Json::Value send_to_map;
	send_to_map["account"] = m_Account;
	send_to_map["playerId"] = m_PlayerId;
	send_to_map["heroId"] = heroid;
	send_to_map["heroSkinId"] = GetHeroSkinId();
	send_to_map["msgid"] = G2M_PLAYER_HERO_INFO_CHANGE_REQUEST;

	//std::string m_send = JsonWriter(send_to_map);

	//send_2_game(G2M_PLAYER_HERO_INFO_CHANGE_REQUEST, m_send);
}

//-------------------------------------------------------------------------------
void Player::Revive(uint32_t srcKey, uint32_t tarKey, int32_t reviveType)
{
	_safe_guard_begin;

	if (PlayerState::ps_dead != GetPlayerState())
	{
		return;
	}

	FightProperty& _fp = GetFightProperty();

	int32_t _max_hp = GetFightProperty().GetMaxHp();
	ModifyHp(srcKey, tarKey, -_max_hp);

	uint32_t _tar_key = tarKey;
	if (0 == _tar_key)
	{
		_tar_key = GetKey();
	}

	SetPlayerState(PlayerState::ps_normal);
	BroadcastRevive(srcKey, _tar_key, reviveType, _max_hp);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::BroadcastRevive(uint32_t srcKey, uint32_t tarKey,
	int32_t reviveType, int32_t hp)
{
	_safe_guard_begin;


	Json::Value json_data;
	json_data["msgid"] = c_player_revive_ret;
	json_data["sk"] = srcKey;
	json_data["tk"] = tarKey;
	json_data["rt"] = reviveType;
	json_data["hp"] = hp;

	CSceneInstance* ins = CSceneManager::GetInstance().GetSceneInstance(instance_id());
	if (ins == NULL) {
		return;
	}
	ins->BroadcastViewMessage(*this, c_someone_dead_ret, json_data, 0);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::PlayerHeal(int32_t val)
{
	_safe_guard_begin;

	if (val < 0)
	{
		return;
	}

	FightProperty& _fp = GetFightProperty();

	int32_t _hp = _fp.GetHp();
	_hp += val;

	int32_t _max_hp = GetFightProperty().GetMaxHp();
	if (_hp > _max_hp)
	{
		_hp = _max_hp;
	}

	_fp.SetHp(_hp);

	CSceneInstance::Broadcast(*this, (uint32_t)UpdatePlayerInfoType::Hp);
	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Player::SetHeroId(uint32_t hero_id)
{
	m_HeroId = hero_id;
}

//-------------------------------------------------------------------------------
void Player::LoadSceneFinished(void)
{
	_safe_guard_begin;

	SetPlayerState(PlayerState::ps_in_scene);

	// 这里表示玩家没有登录过游戏，直接传送到主城
	if (this->instance_id() == 0)
	{
		int32_t ret_code = 0;
		common::CVector3 pos;
		CSceneInstance* ins = CSceneManager::GetInstance().AutoChooseScene((uint32_t)SCENE_MAP_ID::MAIN_CITY, pos);
		if (ins != NULL)
		{
			ins->PlayerEnter(*this, pos, 0);
		}
	}
	else
	{
		//如果登录过了，做个容错处理，如果出错了，直接传回主城
		CSceneInstance* ins = CSceneManager::GetInstance().GetSceneInstance(instance_id());
		if (ins == NULL)
		{
			int32_t ret_code = 0;
			common::CVector3 pos;
			CSceneInstance* ins = CSceneManager::GetInstance().AutoChooseScene((uint32_t)SCENE_MAP_ID::MAIN_CITY, pos);
			if (ins != NULL)
			{
				ins->PlayerEnter(*this, pos, 0);
			}
		}
		//给玩家传送到目标场景
		else
		{
			ins->PlayerEnter(*this, position(), 0);
		}
	}


	uint32_t _usrPosId = GetSn();

	//前端测试
	//int32_t _ret = m_player_lua_ptr->RunScriptFunction(LUA_PlayerCommonScript,
	//	LUA_LoadSceneFinished, _usrPosId);

	//loadSceneFinished();

	sendPlayerLoadSceneFinished();

	_safe_guard_end;
}